package com.qbw.app.module.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.lib.util.ScreenUtil;
import com.qbw.app.R;

/*
 *
 * @author QBW
 * @date 2019/8/20
 */
public class GpsSignalView extends LinearLayout {

    private int mSignalValue;
    private int mHorizonalSpace;
    private int mSignalItemWidth;
    private int mSignalItemHeight;
    private int resSignalBg = -1;

    public GpsSignalView(Context context) {
        super(context);
        init(null);
    }

    public GpsSignalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public GpsSignalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        Context context = getContext();
        mSignalItemWidth = ScreenUtil.dp2px(context, 2);
        mSignalItemHeight = ScreenUtil.dp2px(context, 5);
        mHorizonalSpace = ScreenUtil.dp2px(context, 1);
    }

    public void setSignal(int signal) {
        mSignalValue = signal;
        update();
    }

    public void setSignal(int signal, int resId) {
        mSignalValue = signal;
        resSignalBg = resId;
        update();
    }

    private void update() {
        Context context = getContext();
        removeAllViews();
        View view;
        LayoutParams params;
        for (int i = 0; i < mSignalValue; i++) {
            view = new View(context);
            view.setBackgroundResource(resSignalBg > 0 ? resSignalBg : R.drawable.gps_signal_bg);
            params = new LayoutParams(mSignalItemWidth, ((mSignalItemHeight/5) * (i+5)));
            if (i != 0) {
                params.leftMargin = mHorizonalSpace;
            }
            params.gravity= Gravity.BOTTOM;
            addView(view, params);
        }
    }
}
